package aad.storyboard.model;

import java.util.HashMap;

import aad.storyboard.controller.GUIComposer;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.util.Log;

public class ScaleAnimation extends Animation{

	private float scaleX;
	private float scaleY;
	private HashMap<String,String> hmap; 
	private float startingWidth;
	private float startingHeight;
	private Image checkImage;

	/**
	 *  scales an object of intervalX/intervalY at each step, this is calculated by the duration and the extent of the scling
	 */
	public ScaleAnimation(float scaleX, float scaleY, int duration, int time) {
		super(duration,time);
		this.scaleX = scaleX;
		this.scaleY = scaleY;
		hmap = new HashMap<String,String>();
	}

	public ScaleAnimation(float scaleX, float scaleY, int duration) {
		super(duration);
		this.scaleX = scaleX;
		this.scaleY = scaleY;
		hmap = new HashMap<String,String>();
	}

	/**
	 *  performs one interval scaling
	 */
	@Override
	public void animate() {
			for(Image obj : this.images){
				String[] vals = hmap.get(obj.getName()).split("I");
				float intervalX = Float.parseFloat(vals[0]);
				float intervalY = Float.parseFloat(vals[1]);
				float newWidth = obj.getPreciseWidth()+intervalX;
				float newHeight = obj.getPreciseHeight()+intervalY;
				//obj.setPosX(obj.getPreciseX()-(intervalX/2));
				//obj.setPosY(obj.getPreciseY()-(intervalY/2));
				obj.setHeight(newHeight);
				obj.setWidth(newWidth);
				
			}
	}


	/**
	 *  rescales to the original size
	 */
	@Override
	public void undo() {
		float invScaleX = 1/this.scaleX;
		float invScaleY = 1/this.scaleY;
		for(Image obj : this.images){
			float newWidth = obj.getWidth() * invScaleX;
			float newHeight = obj.getHeight() * invScaleY;
			//obj.setPosX(obj.getPreciseX()-((newWidth-obj.getWidth())/2));
			//obj.setPosY(obj.getPreciseY()-((newHeight-obj.getHeight())/2));
			obj.setWidth(newWidth);
			obj.setHeight(newHeight);
		}
		
	}

	@Override
	public void addImage(Image go){
		if(this.images.size() == 0){
			this.checkImage =  go;
			this.startingHeight = go.getHeight();
			this.startingWidth = go.getWidth();
		}
		this.images.add(go);
		this.storeInitialMetrics(go);
	}

	@Override
	public void adjustParameters() {
		this.scaleX = this.startingHeight/this.images.get(0).getWidth();
		this.scaleY = this.startingWidth/this.images.get(0).getHeight();
		
	}
	
	private void storeInitialMetrics(Image obj) {
		
			
			float dx = obj.getWidth() * (scaleX -1);
			float dy = obj.getHeight() * (scaleY -1);
			float intervalX = (float) dx/intervals;
			float intervalY = (float) dy/intervals;

			Log.e("TAG","Dx:"+dx+" Dy"+dy+" intervals"+intervals);
			String toInsert = intervalX+"I"+intervalY;
			hmap.put(obj.getName(), toInsert);
		
	}

}
